# 使用Ubuntu 22.04作为基础镜像，包含C++编译器
FROM ubuntu:22.04 AS builder

# 更换软件源
RUN sed -i 's/archive.ubuntu.com/mirrors.tencentyun.com/g' /etc/apt/sources.list
RUN sed -i 's/security.ubuntu.com/mirrors.tencentyun.com/g' /etc/apt/sources.list

# 更新系统包并安装构建工具和必要的库
RUN apt update
RUN apt install -y \
    g++ \
    make \
    libboost-all-dev \
    libjsoncpp-dev \
    git \
    redis-server \
    libhiredis-dev \
    cmake \
    mysql-server \
    libmysql++-dev

RUN git clone https://gitcode.com/sewenew/redis-plus-plus.git
WORKDIR /redis-plus-plus/build
RUN cmake ..
RUN make
RUN make install

WORKDIR /

RUN git clone https://gitcode.com/nlohmann/json.git
RUN cp -r json/include/* /usr/local/include/

RUN rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /usr/src/app

# 将项目源码复制到容器中
COPY . .

# 编译项目
RUN make clean
RUN make

# 暴露端口
EXPOSE 8084

# 启动命令，直接指定可执行文件名
CMD ["./http_server"]

# # 第二阶段构建，用于运行环境，基于较小的基础镜像
# FROM ubuntu:22.04

# # 更换软件源
# RUN sed -i 's/archive.ubuntu.com/mirrors.tencentyun.com/g' /etc/apt/sources.list
# RUN sed -i 's/security.ubuntu.com/mirrors.tencentyun.com/g' /etc/apt/sources.list

# # 更新系统包并安装构建工具和必要的库
# # RUN apt-get update && apt-get install -y \
# #     libboost-all-dev \
# #     libjsoncpp-dev \
# #     redis-server \
# #     libhiredis-dev \
# #     mysql-server \
# #     libmysql++-dev

# RUN apt-get update && apt-get install -y \
#     libboost-all-dev \
#     libjsoncpp-dev \
#     redis-server \
#     libhiredis-dev \
#     mysql-server \
#     libmysql++-dev \
#     mysql-client \
#     libmysqlclient-dev

# # 设置工作目录
# WORKDIR /usr/src/app

# # 从构建阶段拷贝二进制文件到运行阶段
# COPY --from=builder /usr/src/app/http_server /usr/local/bin/
# COPY --from=builder /usr/src/app/parser /usr/local/bin/

# # 设置环境变量
# ENV REDIS_HOST=localhost
# ENV MYSQL_HOST=mysql
# ENV MYSQL_PORT=3306
# # 更多环境变量...

# # 暴露端口
# EXPOSE 8080

# # 启动命令，直接指定可执行文件名
# CMD ["./http_server"]